package com.jieshuibao.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.UserDealRecordMapper;
import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.mapper.WishbuyExMapper;
import com.jieshuibao.mapper.WishbuyMapper;
import com.jieshuibao.mapper.WishcodeMapper;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.UserDealRecord;
import com.jieshuibao.model.db.WishExBuy;
import com.jieshuibao.model.db.Wishbuy;
import com.jieshuibao.model.db.WishbuyExample;
import com.jieshuibao.model.db.Wishcode;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class WishbuyService{
	
	@Resource
	private WishbuyMapper wishbuyMapper;
	
	@Resource
	private WishbuyExMapper wihsExMapper;
	
	@Resource
	private UserDealRecordMapper userDealRecordMapper;
	
	@Resource
	private WishcodeMapper wishcodeMapper;
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private RedisService redisService;
	
	public void addWishbuy(Wishbuy wishbuy) {
		wishbuyMapper.insertSelective(wishbuy);
	}
	
	public void delWishbuyById(String id) {
		wishbuyMapper.deleteByPrimaryKey(id);
	}
	
	public void updateWishbuy(Wishbuy wishbuy) {
		wishbuyMapper.updateByPrimaryKeySelective(wishbuy);
	}
	
	public Wishbuy getWishbuyById(String id) {
		return wishbuyMapper.selectByPrimaryKey(id);
	}
	
	public List<Wishbuy> getPageWishbuy(WishbuyExample wishbuyExample) {
		return wishbuyMapper.selectPage(wishbuyExample);
	}
	
	public int countWishbuy(WishbuyExample wishbuyExample) {
		return wishbuyMapper.countByExample(wishbuyExample);
	}
	
	public List<WishExBuy> getWishBuyUserPage(WishbuyExample wishbuyExample){
		return wihsExMapper.selectWishExBuysPage(wishbuyExample);
	}
	public Integer getSumBuyCount(String id){
		return wihsExMapper.selectSumBuyCount(id);
	}
	public void updateAfterWish(Wishbuy wishbuy, List<Wishcode> codeList, UserDealRecord userDealRecord,UserDealRecord userDealRecord1) {
		wishbuyMapper.updateByPrimaryKeySelective(wishbuy);
		for (Wishcode wishcode : codeList) {
			wishcodeMapper.insert(wishcode);
		}
		userDealRecordMapper.insert(userDealRecord);
		userDealRecordMapper.insert(userDealRecord1);
	}
	//愿望购买
	
		public void updateUserAndAddWishBuy(User user,Wishbuy wishbuy){
			if(user!=null&&wishbuy!=null){
				Integer buyCount = wishbuy.getBuyCount();
				String payType = wishbuy.getPayType();
				
				String phoneNumber = user.getPhoneNumber();
				String id = wishbuy.getId();
				Integer money = wishbuy.getMoney();
				String wishId = wishbuy.getWishId();
				for (int i = 0; i <buyCount; i++) {
					Wishcode wishcode = new Wishcode();
					String wishcodeid = UUIDGenerator.getUUID();
					wishcode.setId(wishcodeid);
					wishcode.setBuywithId(id);
					wishcode.setUserId(phoneNumber);
					Long incrby = redisService.getIncrby(wishId+"_code");
					String valueOf = String.valueOf(incrby);
					wishcode.setWishId(wishId);
					wishcode.setCode(valueOf);
					wishcodeMapper.insertSelective(wishcode);
				}
				userMapper.updateByPrimaryKeySelective(user);
				wishbuyMapper.insertSelective(wishbuy);
				//流水 
				UserDealRecord userDealRecord1 = new UserDealRecord();
				UserDealRecord userDealRecord2 = new UserDealRecord();
				Date date = new Date();
				String uuid1 = UUIDGenerator.getUUID();//用户支出流水
				String uuid2 = UUIDGenerator.getUUID();//公司收入流水
				userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
				userDealRecord1.setBusinessNo(id);
				userDealRecord1.setCreateDate(date);
				userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_WISHS);
				userDealRecord1.setPaySucTime(date);
				userDealRecord1.setPayType(payType);
				userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				userDealRecord1.setSum((double)money);
				userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
				userDealRecord1.setUserId(phoneNumber);
				userDealRecord1.setUuid(uuid1);
				
				userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
				userDealRecord2.setBusinessNo(id);
				userDealRecord2.setCreateDate(date);
				userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_WISHS);
				userDealRecord2.setPaySucTime(date);
				userDealRecord2.setPayType(payType);
				userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				userDealRecord2.setSum((double)money);
				userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
				userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
				userDealRecord2.setUuid(uuid2);
				userDealRecordMapper.insertSelective(userDealRecord1);
				userDealRecordMapper.insertSelective(userDealRecord2);
				
			}
			
		}
}
